package codeflyer.server.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author CodeFlyer
 * @date May 5, 2020
 * @version 1.0
 */

//一个路径可以对应多个servlet,所有用一个list来存储多个servlet
public class AnalysisXml {
	
	private static Map<String, String> snup = new HashMap<>(); //servet名称和urlPattern
	private static Map<String, String> fnup = new HashMap<>(); //过滤器名称和urlPattern
	private static Map<String, List<Map<String, List<String>>>> hsmapping = new HashMap<>();  //xml对应的路径和servlet键值对
	private static Map<String, List<Map<String, List<String>>>> hfmapping = new HashMap<>();  //xml里的过滤器
	private static List<String> listenerList = new ArrayList<>();				//xml里所有的listener
	
	public static Map<String, List<Map<String, List<String>>>> getHsmapping() {
		return hsmapping;
	}
	

	public static Map<String, List<Map<String, List<String>>>> getHfmapping() {
		return hfmapping;
	}


	public static List<String> getListenerList() {
		return listenerList;
	}


	private static Document load(String filename) {  
	    Document document = null;  
	    try {  
	        SAXReader saxReader = new SAXReader();  
	        document = saxReader.read(new File(filename)); // 读取XML文件,获得document对象  
	    } catch (Exception ex) {  
	        ex.printStackTrace();  
	    }  
	    return document;  
	}
	
	
	public static void analysisXml(String fileName,String project) {
		//获取document对象
		Document document = load(fileName);
		//获得根元素
		Element root = document.getRootElement();
		
		//获得根元素下所有的servlet-mapping
		List<Element> smList = root.elements("servlet-mapping");
		//遍历所有的sm然后把路径和映射信息提取出来
		for (Element element : smList) {  //路径不是唯一的,但是sN一定必须是唯一的
			String sName = element.elements().get(0).getText();  //获得servlet-name
			String uPattern = element.elements().get(1).getText();  //获得url-pattern
			snup.put(sName, uPattern);
		}
		
		//获得根元素下所有的servlet,并把url-pattern和class-name放入map中
		List<Element> sList = root.elements("servelet"); 
		for (Element element : sList) {
			String sN = element.elements().get(0).getText();  //获得servlet-name
			String cName = element.elements().get(1).getText();  //获得class-name
			String uP = snup.get(sN);  //把对应的路径获取到
			if(hsmapping.get(project)!=null) {
				List<Map<String, List<String>>> list = hsmapping.get(project);  //把class的名称放入list中
				for (Map<String, List<String>> map : list) {
					map.get(uP).add(cName);
				}
			}else {
				List<Map<String, List<String>>> list = new ArrayList<>();
				Map<String, List<String>> map = new HashMap<>();
				List<String> cNList = new ArrayList<>();  //没有则创建
				cNList.add(cName);
				map.put(uP, cNList);
				list.add(map);
				hsmapping.put(project, list);
				//hsmapping.put(uP, cNList);		
			}			
		}
		
		//---------------------------servlet的解析结束------------------------------
		
		//获得根元素下所有的servlet-mapping
		List<Element> fmList = root.elements("filter-mapping");
		//遍历所有的sm然后把路径和映射信息提取出来
		for (Element element : fmList) {  //路径不是唯一的,但是sN一定必须是唯一的
			String fName = element.elements().get(0).getText();  //获得servlet-name
			String uPattern = element.elements().get(1).getText();  //获得url-pattern
			snup.put(fName, uPattern);
		}
		
		//获得根元素下所有的servlet,并把url-pattern和class-name放入map中
		List<Element> fList = root.elements("filter"); 
		for (Element element : fList) {
			String fN = element.elements().get(0).getText();  //获得servlet-name
			String cName = element.elements().get(1).getText();  //获得class-name
			String uP = snup.get(fN);  //把对应的路径获取到
			if(hfmapping.get(project)!=null) {
				List<Map<String, List<String>>> list = hfmapping.get(project);  //把class的名称放入list中
				for (Map<String, List<String>> map : list) {
					map.get(uP).add(cName);
				}
			}else {
				List<Map<String, List<String>>> list = new ArrayList<>();
				Map<String, List<String>> map = new HashMap<>();
				List<String> cNList = new ArrayList<>();  //没有则创建
				cNList.add(cName);
				map.put(uP, cNList);
				list.add(map);
				hfmapping.put(project, list);
				//hsmapping.put(uP, cNList);		
			}			
		}
		//---------------------------filter的解析结束------------------------------		
				
		//获得根元素下所有的servlet-mapping
		List<Element> lList = root.elements("listener");
		//遍历所有的sm然后把路径和映射信息提取出来
		for (Element element : lList) {  //路径不是唯一的,但是sN一定必须是唯一的
			String lName = element.elements().get(0).getText();  //获得listener的class路径
			listenerList.add(lName);
		}
		
		//---------------------------listener的解析结束------------------------------	
	}

}


